/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */
package remix.myplayer.misc.floatpermission.rom;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.Method;

public class MeizuUtils {

  private static final String TAG = "MeizuUtils";

  /**
   * 检测 meizu 悬浮窗权限
   */
  public static boolean checkFloatWindowPermission(Context context) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
      return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
    }
    return true;
  }

  /**
   * 去魅族权限申请页面
   */
  public static void applyPermission(Context context) {
    Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
    intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");
    intent.putExtra("packageName", context.getPackageName());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  @TargetApi(Build.VERSION_CODES.KITKAT)
  private static boolean checkOp(Context context, int op) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
      AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
      try {
        Class clazz = AppOpsManager.class;
        Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
        return AppOpsManager.MODE_ALLOWED == (int) method
            .invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
      } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
      }
    } else {
      Log.e(TAG, "Below API 19 cannot invoke!");
    }
    return false;
  }
}
